//MINI UI解析
mini.parse();

//广告配置处理器类
function AdvCfgHandler() {
	this.sacType = mini.get("sacType");
	this.sacName = mini.get("sacName");
	this.sacPage = mini.get("sacPage");
	this.sacPosition = mini.get("sacPosition");
	this.sacState = mini.get("sacState");
	this.advCfgGrid = mini.get("advCfgGrid");
	
	this.isExpanded = false;
	this.acCtl = mini.get("acCtl");
	this.tbForm = $("#toolbarForm");
	this.acPanel = mini.get("advConfigPanel");
	
	this.wmCfg = new WebMgrConfig();
	this.queryType = this.wmCfg.advType[0].value;
	this.queryName = "";
	this.queryPage = this.wmCfg.normalPage[0].value;
	this.queryPosition = this.wmCfg.advPosition[0].value;
	this.queryState = this.wmCfg.normalState[0].value;
	
	this.advCfgGrid.url = __root + "/getAdvConfigs.do";
	
	this.loadSacType = function() {
		this.sacType.setData(this.wmCfg.advType);
		this.sacType.setValue(this.wmCfg.advType[0].value);
	};
	
	this.loadSacPage = function() {
		this.sacPage.setData(this.wmCfg.normalPage);
		this.sacPage.setValue(this.wmCfg.normalPage[0].value);
	};
	
	this.loadSacPosition = function() {
		this.sacPosition.setData(this.wmCfg.advPosition);
		this.sacPosition.setValue(this.wmCfg.advPosition[0].value);
	};
	
	this.loadSacState = function() {
		this.sacState.setData(this.wmCfg.normalState);
		this.sacState.setValue(this.wmCfg.normalState[0].value);
	};
	
	this.loadAdvCfgGrid = function() {
		var param = {
			sacType : this.queryType, 
			sacName : this.queryName, 
			sacPage : this.queryPage, 
			sacPosition : this.queryPosition, 
			sacState : this.queryState};
		this.advCfgGrid.load(param);
	};
	
	this.findType = function(value) {
		var type = null;
		
		for (var index = 0; index < this.wmCfg.advType.length; index += 1) {
			var temp = this.wmCfg.advType[index];
			if (temp.value == value) {
				type = temp;
				break;
			}
		}
		
		return type;
	};
	
	this.findPage = function(value) {
		var page = null;
		
		for (var index = 0; index < this.wmCfg.normalPage.length; index += 1) {
			var temp = this.wmCfg.normalPage[index];
			if (temp.value == value) {
				page = temp;
				break;
			}
		}
		
		return page;
	};
	
	this.findPosition = function(value) {
		var position = null;
		
		for (var index = 0; index < this.wmCfg.advPosition.length; index += 1) {
			var temp = this.wmCfg.advPosition[index];
			if (temp.value == value) {
				position = temp;
				break;
			}
		}
		
		return position;
	};
	
	this.findState = function(value) {
		var state = null;
		
		for (var index = 0; index < this.wmCfg.normalState.length; index += 1) {
			var temp = this.wmCfg.normalState[index];
			if (temp.value == value) {
				state = temp;
				break;
			}
		}
		
		return state;
	};
	
	this.changeToYes = function(id) {
		this.switchState(id, true);
	};
	
	this.changeToNo = function(id) {
		this.switchState(id, false);
	};
	
	this.switchState = function(id, to) {
		var param = { sacId : id, sacTo : to};
		var json = mini.encode(param);
		$.ajax({
			url : __root + "/switchAcState.do", 
			type : "POST", 
			data: {
	        	data: json,
	        	lslUser: gbOperateUser,
	    		lslAddress: gbOperateIp
	        },
			dataType : "json", 
			success : function(result) {
				instance.loadAdvCfgGrid();
			}, 
			error : function (request) {
				mini.alert(request.responseText, "系统提示");
			}
		});
	};
	
	this.edit = function(id, name) {
		miniWin.afreshNewWin();
		miniWin.setParams(688, 470, "编辑配置[<font color='blue'>" + name + "</font>]");
		miniWin.setOption("bodyStyle","padding:0px;");
		
		miniWin.load("editAdvCfg.jsp", function() {
			var iframe = miniWin.getIFrameEl();
	        var data = { isedit : true, sacId : id };
	        iframe.contentWindow.instance.load(data);
		}, function(action) {
			if (action == "save") {
				instance.loadAdvCfgGrid();
			}
		});
		
		miniWin.show();
	};
	
	this.del = function(id) {
		mini.confirm("确定要删除选定的配置？", "系统提示", function(action) {
			if (action == "ok") {
				var ids = [{ sacId : id }];
				instance.removes(ids);
			}
		});
	};
	
	this.removes = function(ids) {
		var json = mini.encode(ids);
		$.ajax({
			url : __root + "/delAdvConfigs.do", 
			type : "POST", 
			data: {
	        	data: json,
	        	lslUser: gbOperateUser,
	    		lslAddress: gbOperateIp
	        },
			dataType : "json", 
			success : function(result) {
				instance.loadAdvCfgGrid();
			}, 
			error : function (request) {
				mini.alert(request.responseText, "系统提示");
			}
		});
	};
	
	this.doLayout = function() {
		instance.acPanel.doLayout();
	};
};

var instance = new AdvCfgHandler();		//创建广告配置处理器全局实例
instance.loadSacType();					//加载内容类型
instance.loadSacPage();					//加载显示页面
instance.loadSacPosition();				//加载显示位置
instance.loadSacState();				//加载配置状态
instance.loadAdvCfgGrid();				//加载配置表格

//定义新建配置点击事件处理程序
function onAcNewClick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(688, 470, "新建配置");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	miniWin.load("editAdvCfg.jsp", function() {
		var iframe = miniWin.getIFrameEl();
        var data = { isedit : true, sacId : null };
        iframe.contentWindow.instance.load(data);
	}, function(action) {
		if (action == "save") {
			instance.loadAdvCfgGrid();
		}
	});
	
	miniWin.show();
}

//定义查询控制点击事件处理程序
function onAcCtlClick(e) {
	if (instance.isExpanded) {
		instance.isExpanded = false;
		instance.acCtl.setText("展开查询");
		instance.acCtl.setIconCls("icon-btn-expand");
	} else {
		instance.isExpanded = true;
		instance.acCtl.setText("折叠查询");
		instance.acCtl.setIconCls("icon-btn-collapse");
	}
	
	instance.tbForm.toggle(500);
	setTimeout(instance.doLayout, 500);
}

//定义查询点击事件处理程序
function onAcQueryClick(e) {
	instance.queryType = instance.sacType.getValue();
	instance.queryName = instance.sacName.getValue();
	instance.queryPage = instance.sacPage.getValue();
	instance.queryPosition = instance.sacPosition.getValue();
	instance.queryState = instance.sacState.getValue();
	
	instance.loadAdvCfgGrid();
}

//定义刷新点击事件处理程序
function afresh(e) {
	//执行查询
	onAcQueryClick();
	//重置布局
	instance.doLayout();
}

//定义广告配置表格行双击事件处理程序
function onAdvCfgGridRowDbclick(e) {
	miniWin.afreshNewWin();
	miniWin.setParams(688, 470, "配置明细[<font color='blue'>" + e.record.sacName + "</font>]");
	miniWin.setOption("bodyStyle","padding:0px;");
	
	miniWin.load("editAdvCfg.jsp", function() {
		var iframe = miniWin.getIFrameEl();
        var data = { isedit : false, sacId : e.record.sacId };
        iframe.contentWindow.instance.load(data);
	});
	
	miniWin.show();
}

//定义广告配置表格单元格绘制事件处理程序
function onAdvCfgGridDrawCell(e) {
	var html = "";
	
	switch (e.column.field) {
		case "sacType":
		{
			var type = instance.findType(e.record.sacType);
			if (type) {
				html += type.text;
			}
			break;
		}
		case "sacPage":
		{
			var page = instance.findPage(e.record.sacPage);
			if (page) {
				html += page.text;
			}
			break;
		}
		case "sacPosition":
		{
			var position = instance.findPosition(e.record.sacPosition);
			if (position) {
				html += position.text;
			}
			break;
		}
		case "sacState":
		{
			var state = instance.findState(e.record.sacState);
			if (state) {
				html += "<span><div class='" + state.cls + "' title='" + state.text + 
					"' onclick='instance." + state.method + "(\"" + e.record.sacId + "\");'></div></span>";
			}
			break;
		}
		case "sacOperator":
		{
			var state = instance.findState(e.record.sacState);
			if (state) {
				var id = e.record.sacId;
				
				html += "<span><a style='cursor: pointer;' onclick='instance.edit(\"" + id + 
					"\", \"" + e.record.sacName + "\");'>编辑</a></span>";
				html += "<span>&nbsp;|&nbsp;</span>";
				if (state.deleted) {
					html += "<span><a style='cursor: pointer;' onclick='instance.del(\"" + id + "\");'>删除</a></span>";
				} else {
					html += "<span><a style='cursor: default; color: gray;'>删除</a></span>";
				}
			}
			break;
		}
	}
	
	if (html) e.cellHtml = html;
}
